// pages/qualityControl/definQuality/edtionDefin/index.js
import WxValidate from '../../../../utils/validate'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    serialOrLotNumber:"",
    qcPeriod:"",
    qcItemCategory:"",
    qcItem:"",
    qcSubItem:"",
    thresholdRange:"",
    unit:"",
    chartDisplay:"",
    chartDisplayText:"",
    charttypesValue:"",
    useQcEquipment:"",
    param:{},
    array3: [],
    value3:0,
    charttypes:[],
    charttypesV:0,
    arr1:["是","否"],
    arr4:['日检','月检','季检','年检'],
    value4:0,
    value1:0,
    arr2:[],
    value2:0,
  },
  change(e){
    console.log(e)
    this.setData({
      qcItem:e.detail.value
    })
  },
  changeqcSubItem(e){
    this.setData({
      qcSubItem:e.detail.value
    })
  },
  changethresholdRange(e){
    this.setData({
      thresholdRange:e.detail.value
    })
  },
  changeunit(e){
    this.setData({
      unit:e.detail.value
    })
  },
  initValidate() {
    const rules = {
      serialOrLotNumber:{
        required: true,
      },
      qcPeriod:{
        required: true,
      },
      qcItemCategory:{
        required: true,
      },
      qcItem:{
        required: true,
      },
      chartDisplay:{
        required: true,
      },
      useQcEquipment:{
        required: true,
      },

    }
    const messages = {
      serialOrLotNumber:{
        required: '请填写设备编号',
      },
      qcPeriod:{
        required: '请填写质控周期',
      },
      qcItemCategory:{
        required: '请填写质控项分类',
      },
      qcItem:{
        required: '请填写质控项',
      },
      chartDisplay:{
        required: '请填写图表展示形式',
      },
      useQcEquipment:{
        required: '请填写是否质控设备',
      },
    }
    let that = this;
    this.WxValidate = new WxValidate(rules, messages)
    // this.WxValidate.addMethod('limitInput',(value, param)=>{
    //   return  this.WxValidate.optional(value) || /(?=.*[0-9])(?=.*[A-Z])(?=.*[a-z])(?=.*[^a-zA-Z0-9])/.test(value);
    // },'密码包含大小写英文数字和符号')
  },
    // 展示信息
    showModal(error){
      wx.showModal({
        content: error.msg,
        showCancel: false,
      })
    },
  save(){
    let self = this
    let obj={
      serialOrLotNumber:self.data.serialOrLotNumber,
      qcPeriod:self.data.qcPeriod,
      qcItemCategory:self.data.qcItemCategory,
      qcItem:self.data.qcItem,
      qcSubItem:self.data.qcSubItem,
      thresholdRange:self.data.thresholdRange,
      unit:self.data.unit,
      chartDisplay:self.data.chartDisplay,
      useQcEquipment:self.data.useQcEquipment=="是"?1:0,
      id:self.data.param.item.id
    }
    if(!this.WxValidate.checkForm(obj)){
      let msg = this.WxValidate.errorList[0];
      this.showModal(msg);
      return false
    }
    self
    .axios
    .addOrUpdate('QcProjectEquipmentConfig', obj, true)
    .then(() => {
      wx.showToast({
        title: '保存成功',
        icon: 'succes',
        duration: 1000,
        mask:true
    })
      wx.navigateBack({
        delta: 2
    })
    })
    .catch((err) => {

    })
    console.log(obj)
  },
  bindPicker3Change: function(e) {
    this.setData({
        serialOrLotNumber:this.data.array3[e.detail.value]
    })
  },
  bindPickerzq: function(e) {
    let self = this
    this.setData({
      qcPeriod: self.data.arr4[e.detail.value]
    })
  },
  bindPickerzksb:function(e){
    let self = this
    this.setData({
      useQcEquipment: self.data.arr1[e.detail.value]
    })
  },
  bindPickertb: function(e) {
    let self = this
    this.setData({
      chartDisplayText: self.data.charttypes[e.detail.value],
      chartDisplay:self.data.charttypesValue[e.detail.value]
    })
  },
  bindPickerzkfl: function(e) {
    let self = this
    this.setData({
      qcItemCategory: self.data.arr2[e.detail.value]
    })
  },
  getequiment(){
    this
    .axios
    .custom("AllInstalledProducts", "serialOrLotNumbers", {
      id: this.Store.getItem("userid"),
    })
    .then((result) => {
      console.log(result);
      this.setData({
        array3:result.data
      })
    })
    .catch((err) => {
      self.$notify(err.data.msg, "error");
    });
  },
    getDataDict() {
    let self = this;
  let id = this.Store.getItem("customerId");
  let customerId=""
  let customerKey=""
  customerId =
    id != "undefined" ? this.Store.getItem("customerId") : "";

  if (id != "undefined") {
    customerKey = id != "10001" ? "UserDataDict" : "DataDict";
  } else {
    customerKey = "DataDict";
  }
  let obj = {};
  obj.typeValue = "qci_category";
  obj.customerId = customerId;
    self
      .axios
      .query("SystemEnum", {}, { typeValue: "qc_cycle" })
      .then(function (response) {
        console.log(response)
      });
    self
      .axios
      .query(customerKey, {}, obj)
      .then(function (response) {
        console.log(response)
        let arr=[]
        response.data.rows.forEach(e=>{
          arr.push(e.text)
        })
        self.setData({
          arr2:arr
        })
        arr.forEach((e,index)=>{
          if(e=='自定义质控项'){
            self.setData({
              value2:index
            })
            return
          }
        })
      });
    self
      .axios
      .query("DataDict", {}, { typeValue: "qc_chart_type" })
      .then(function (response) {
        console.log(response)
        let arr=[]
        let arr1=[]
       response.data.rows.forEach(e=>{
            arr.push(e.text)
            arr1.push(e.value)
       })
      self.setData({
      charttypes:arr,
      charttypesValue:arr1
     })
        // self.charttypes = response.rows;
      });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let self = this
    this.getequiment()
    this.getDataDict()
    this.initValidate();
     const eventChannel = this.getOpenerEventChannel();
		// 监听 index页面定义的toHome 事件
		eventChannel.on('toedtion', (res) => {
      this.setData({
        serialOrLotNumber:res.item.serialOrLotNumber,
        qcPeriod:res.item.qcPeriod,
        qcItemCategory:res.item.qcItemCategory,
        qcItem:res.item.qcItem,
        qcSubItem:res.item.qcSubItem,
        thresholdRange:res.item.thresholdRange,
        unit:res.item.unit,
        chartDisplayText:res.item.chartDisplayText,
        chartDisplay:res.item.chartDisplay,
        useQcEquipment:res.item.useQcEquipment==1?"是":"否",
        param:res
      })
      console.log(res,'页面传值')
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})